package com.hainatong.shiphelper.module.cert;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentCertMgrBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.cert.adapter.CertAdapter;
import com.hainatong.shiphelper.module.cert.entity.ShipCertBean;
import com.hainatong.shiphelper.module.cert.viewmodel.CertViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;


/**
 * 证书 管理
 */
public class CertMgrFragment extends MvvmLazyFragment<FragmentCertMgrBinding, CertViewModel>
{

    private CertAdapter adapter;

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_cert_mgr;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
       adapter = new CertAdapter();
        // 搜索
        viewDataBinding.btnSearch.setOnClickListener( v -> {

            String keyWord = viewDataBinding.etSearch.getText().toString().trim();
            if(ObjectUtils.isNotEmpty( keyWord ))
            {
                searchShipCertByName( keyWord );
                KeyboardUtils.hideSoftInput( getActivity() );
            }
        } );
        // 软键盘 上的 搜索 按钮
        viewDataBinding.etSearch.setOnKeyListener( (v, keyCode, event) -> {
            if(keyCode == KeyEvent.KEYCODE_ENTER)
            {
                String keyWordStr = viewDataBinding.etSearch.getText().toString().trim();
                if(!TextUtils.isEmpty( keyWordStr ))
                {
                    KeyboardUtils.hideSoftInput( getActivity());
                    searchShipCertByName( keyWordStr );
                }
                return  true;
            }
            return false;
        } );

        viewDataBinding.refreshlayout.setRefreshHeader( new ClassicsHeader(getActivity()));
        viewDataBinding.refreshlayout.setRefreshFooter( new ClassicsFooter(getActivity()));
        viewDataBinding.refreshlayout.setOnRefreshListener(refreshLayout -> {
           viewModel.tryRefresh();
        });
        viewDataBinding.refreshlayout.setOnLoadMoreListener( refreshLayout -> {
           viewModel.loadMore();
        } );
        viewDataBinding.recyclerview.setLayoutManager( new LinearLayoutManager(getActivity()));
        viewDataBinding.recyclerview.hasFixedSize();
        viewDataBinding.recyclerview.setAdapter( adapter );

        adapter.setOnItemClickListener( (adapter1, view1, position) -> {

            ShipCertBean certTemp = adapter.getItem( position );

            ComFragmentActivity.startViewByParam(getActivity(),Constants.VIEW_CERT_DETAIL, GsonUtil.obj2json( certTemp ));

        } );

    }


    @Override
    protected void lazyLoad()
    {
        //查 船舶 证书
        viewModel.queryShipCert("");
        viewModel.getShipCertListResult().observe( this,result -> {

            if( result.isSuccess() )
            {
                if( result.isRefresh() )
                {
                    viewDataBinding.refreshlayout.finishRefresh(true);
                    if( result.getData() != null )
                    {
                        adapter.setList(result.getData());
                    }
                }
                else
                {
                    viewDataBinding.refreshlayout.finishLoadMore(true);
                    if( result.getData() != null )
                    {
                        adapter.addData(result.getData());
                    }
                }
            }
            else
            {
                if( result.isRefresh() )
                {
                    //刷新时 加载失败
                    ToastUtils.showShort( result.getMsg() );
                }
                else
                {
                    if( result.isLoadMoreEmpty() )
                    {
                        viewDataBinding.refreshlayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        } );
    }

    /**
     *  关键字 搜索
     * @param keyName
     */
    private void searchShipCertByName(String keyName)
    {
        viewModel.queryShipCert( keyName );
    }


}
